/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.yihu.wlyy.service.common.account;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.utils.Clock;

import com.yihu.wlyy.entity.doctor.Doctor;
import com.yihu.wlyy.entity.Token;
import com.yihu.wlyy.repository.DoctorDao;
import com.yihu.wlyy.util.EncodesUtil;

/**
 * 医生基本信息类.
 *
 * @author George
 */
// Spring Service Bean的标识.
@Component
@Transactional(rollbackFor = Exception.class)
public class DoctorService extends TokenService {
	@Autowired
	private DoctorDao doctorDao;
	private Clock clock = Clock.DEFAULT;

	public Doctor findDoctorByCode(String code) {
		return doctorDao.findByCode(code);
	}

	public Doctor findDoctorByMobile(String mobile) {
		return doctorDao.findByMobile(mobile);
	}

	public JSONObject registerDoctor(Doctor doctor, String imei) throws Exception {
		EncodesUtil.entryptPassword(doctor);
		doctor.setCode(getCode());
		doctor.setCzrq(clock.getCurrentDate());

		Doctor temp = doctorDao.save(doctor);
		if (temp != null) {
			//生成登录tokey
			Token token = newToken(doctor.getCode(), imei, 2);
			if (token != null) {
				//生成登录信息成功
				JSONObject json = new JSONObject();
				json.put("uid", doctor.getCode());
				json.put("token", token.getToken());
				return json;
			}
		}
		return null;
	}

	/**
	 * 修改医生密码
	 */
	public void updateDoctorPwd(Doctor doctor) {
		EncodesUtil.entryptPassword(doctor);
		doctorDao.save(doctor);
	}



}
